package org.dev.biz_order

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import org.dev.`as`.project.common.http.ApiFactory
import org.dev.biz_order.address.Address
import org.dev.biz_order.address.AddressApi
import org.dev.biz_order.address.AddressModel
import org.dev.hi.library.restful.HiCallback
import org.dev.hi.library.restful.HiResponse

/**
 * Author WenPing
 * CreateTime 2021/9/4.
 * Description:
 */
class OrderViewModel : ViewModel() {

    fun queryMainAddress(): LiveData<Address?> {
        val liveData = MutableLiveData<Address>()
        ApiFactory.create(AddressApi::class.java)
            .queryAddress(
                1, 1
            )
            .enqueue(object : HiCallback<AddressModel> {
                override fun onSuccess(hiResponse: HiResponse<AddressModel>) {
                    val list = hiResponse.data?.list
                    val firstElement = if (list?.isNotEmpty() == true) list[0] else null
                    liveData.postValue(firstElement)
                }

                override fun onFailed(throws: Throwable) {
                    // 请求数据失败
                    liveData.postValue(null)
                }
            })
        return liveData
    }

}